package com.sinosoft.jwjw.component.service.imp.system;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sinosoft.jwjw.base.BaseCrudMapper;
import com.sinosoft.jwjw.base.BaseCrudServiceImp;
import com.sinosoft.jwjw.component.mapper.system.SDemoMapper;
import com.sinosoft.jwjw.component.model.system.SDemo;
import com.sinosoft.jwjw.component.service.system.SDemoService;
import com.sinosoft.jwjw.utils.ResultDetail;
import com.sinosoft.jwjw.utils.ResultPage;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: ericliu
 * @date: 2019/2/16
 * @description:
 */
@Service
public class SDemoServiceImp extends BaseCrudServiceImp<SDemo, String> implements SDemoService {

    @Autowired
    SDemoMapper sDemoMapper;

    @Override
    public BaseCrudMapper getMapper() {
        return sDemoMapper;
    }

    @Override
    public SDemo selectByPrimaryKey(String id) {
        return sDemoMapper.selectByPrimaryKey(id);
    }


    @Override
    public ResultDetail inserts(SDemo sDemo) {
        ResultDetail result = new ResultDetail();

        int rows = sDemoMapper.insert(sDemo);
        if(rows > 0){
            result.setCode(0);
            result.setMsg("保存成功！");
            result.setInnermsg("");
            result.setResult(null);
        }
        else{
            result.setCode(500);
            result.setMsg("保存失败！");
            result.setInnermsg("");
            result.setResult(null);
        }
        return result;
    }

    @Override
    public ResultDetail updateBaseInfoById(Map<String,Object> params) {
        ResultDetail result = new ResultDetail();

        int rows = sDemoMapper.updateById(params);
        if(rows > 0){
            result.setCode(0);
            result.setMsg("保存成功！");
            result.setInnermsg("");
            result.setResult(null);
        }
        else{
            result.setCode(500);
            result.setMsg("保存失败！");
            result.setInnermsg("");
            result.setResult(null);
        }
        return result;
    }

    @Override
    public SDemo selectById(String id) {
        return sDemoMapper.selectByPrimaryKey(id);
    }

    @Override
    public ResultDetail deleteById(String id) {

        ResultDetail result = new ResultDetail();

        Map<String,Object> params = new HashMap<>();
        params.put("id",id);
        params.put("isDel",1);

        int rows = sDemoMapper.deleteById(id);
        if(rows > 0){
            result.setCode(0);
            result.setMsg("删除成功！");
            result.setInnermsg("");
            result.setResult(null);
        }
        else{
            result.setCode(500);
            result.setMsg("删除失败！");
            result.setInnermsg("");
            result.setResult(null);
        }
        return result;
    }

    /**
     * 查找用户列表
     * @param pageIndex
     * @param pageSize
     * @param chineseName
     * @return
     */
    @Override
    public ResultPage getListPage(int pageIndex, int pageSize , String chineseName) {
        ResultPage resultPage = new ResultPage();

        try {
            Map<String,Object> params = new HashMap<>();
            params.put("chineseName",chineseName);
            params.put("isState",1);

            Page page = PageHelper.startPage(pageIndex, pageSize, true);
            JSONArray list = sDemoMapper.getListPage(params);

            resultPage.setCode(0);
            resultPage.setMsg("成功");
            resultPage.setPageIndex(pageIndex);
            resultPage.setPageSize(pageSize);
            resultPage.setAllcount(page.getTotal());
            resultPage.setResult(list);

        } catch (Exception e) {

            resultPage.setCode(500);
            resultPage.setMsg("呀，网络不太好~");
            resultPage.setInnermsg(e.toString());
            resultPage.setPageIndex(pageIndex);
            resultPage.setPageSize(pageSize);
            resultPage.setAllcount(0L);
            resultPage.setResult(new JSONArray());

        } finally {
            return resultPage;
        }
    }
}
